package com.changzhi.controller;

import com.alibaba.fastjson.JSON;
import com.changzhi.common.mq.MqProducer;
import com.changzhi.common.util.TokenUtil;
import com.changzhi.ldyl.model.UserGift;
import com.changzhi.mapper.ActivityDataMapper;
import com.changzhi.mapper.UserGiftMapper;
import com.changzhi.service.*;
import com.changzhi.util.CommonUtil;
import com.changzhi.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description:测试
 * @author: 小落
 * @date: 2020年10月10日
 */
@Slf4j
@RestController
@RequestMapping("test")
public class TestController {

	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private ChatChannelService chatChannelService;
	@Autowired
	private UserGiftDayService userGiftDayService;
	@Resource
	private ActivityDataMapper activityDataMapper;
	@Autowired
	private MqProducer mqProducer;
	@Value("${h5.domain}")
	private String h5Domain;
	@Resource
	private UserGiftMapper userGiftMapper;
	@Autowired
	private SignService signService;
	@Autowired
	private TanabataService tanabataService;

	public static void main(String[] args) {
		Map<String, String> map = new HashMap<>();
		map.put("13", "1281524870147997696");
		map.put("25", "1351854801901584384");
		map.put("26", "1338348217682624512");
		map.put("314743", "1295253461415231488");
		map.put("1066341", "1352101536284540928");
		map.put("1402089", "1372847767998693376");
		map.put("1571550", "1379380363373051904");
		map.put("1572184", "1379394145822441472");
		//
		Map<String, String> result = new HashMap<>();
		Set<Map.Entry<String, String>> entries = map.entrySet();
		for (Map.Entry<String, String> entry : entries) {
			result.put(entry.getKey(), TokenUtil.createToken(entry.getValue(), 100));
			System.out.println(entry.getKey() + "\t\t\t" + TokenUtil.createToken(entry.getValue(), 100));
		}
		System.out.println(JSON.toJSONString(result));
	}

	@PostMapping("savePrize2")
	public Object savePrize2(@RequestBody Map<String,Object> map) {
		CommonUtil.checkPwd((String) map.get("pwd"));
		tanabataService.givePrize();
		return "ok";
	}

	@PostMapping("savePrize")
	public Object savePrize(@RequestBody Map<String,Object> map) {
		CommonUtil.checkPwd((String) map.get("pwd"));
		signService.givePrize();
		return "ok";
	}

	@PostMapping("differGiftValue")
	public Object differGiftValue(@RequestBody Map<String,Object> map) {
		CommonUtil.checkPwd((String) map.get("pwd"));
		return userGiftDayService.differGiftValue();
	}

	/*@PostMapping("pushBeastInfo")
	public Object pushBeastInfo() {
		redisUtil.set(RedisConstans.CARNIVAL_PUSH, 1, RedisConstans.TWO_MINUTES);

		Map<String, Object> beastMap = new HashMap<>();
		beastMap.put("title", "年兽作战");
		beastMap.put("content", String.format("<span style=\"font-size: 13px;font-weight: bold;color: white\"><font color=\"#FFEF39\">%s点</font>的年兽大作战开始啦！丰厚奖励等你来拿！》》前往打年兽</span>", 4));
		beastMap.put("linkValue", h5Domain + "/activityxb/#/newYear?token=${login_token}");
		beastMap.put("linkType", 2);
		mqProducer.sendPushInfo(JSON.toJSONString(beastMap));
		return "ok";
	}

	//初始化题目
	@PostMapping("initTitle")
	public Object initTitle() throws Exception {
		List<ActivityData> list = new ArrayList<>();
		//读取文件
		File file = new File("D:\\补.txt");
		InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file),"UTF-8");
		BufferedReader bf = new BufferedReader(inputReader);
		// 按行读取字符串
		String str;
		String a1;
		String a2;
		Integer result;
		String content = null;
		while ((str = bf.readLine()) != null) {
			System.out.println("=======:"+str);
			if(!str.startsWith("A")){
				String[] split = str.split("\\.");
				content=split[1];
			} else{
				String[] arr = str.split(" ");
				a1 = arr[0].replace("、",".");
				if(arr[0].contains("、")){
					result = 0;
				} else {
					result = 1;
				}
				int b = str.indexOf("B");
				String substring = str.substring(b);
				a2 = substring.replace("、",".");
				ActivityData data = new ActivityData();
				data.setProperty0(a1);
				data.setProperty1(a2);
				data.setResult(result);
				data.setActivityId(9);
				data.setContent(content);
				list.add(data);
			}
		}
		bf.close();
		inputReader.close();
		System.out.println(JSON.toJSONString(list));
		for (ActivityData activityData : list) {
			activityDataMapper.insert(activityData);
		}
		return "ok";
	}

	//初始化拼图背景
	@PostMapping("initBg")
	public Object initBg() throws Exception {
		String[] pathArr = new String[]{
				"D:\\bg\\01",
				"D:\\bg\\02",
				"D:\\bg\\03",
				"D:\\bg\\04",
				"D:\\bg\\05",
				"D:\\bg\\06",
				"D:\\bg\\07",
				"D:\\bg\\08",
				"D:\\bg\\09",
				"D:\\bg\\10"
		};
		int i = 1;
		for (String path : pathArr) {
			ArrayList<String> files = ObsUtil.getFiles(path);
			List<String> list = new ArrayList<>();
			for (String file : files) {
				String fileName = String.format("user/dressup/%s-%s.png", System.currentTimeMillis(), "01");
				String url = ObsUtil.uploadByAvatar(fileName, file);
				System.err.println(url);
				list.add(url);
			}
			int y = 1;
			List<ActivityData> resultList = new ArrayList<>();
			for (String url : list) {
				ActivityData data = new ActivityData();
				data.setProperty0(i+"");
				data.setProperty1(y+"");
				data.setActivityId(6);
				data.setContent(url);
				resultList.add(data);
				y++;
			}
			i++;
			System.out.println("========"+JSON.toJSONString(list));
			for (ActivityData activityData : resultList) {
				activityDataMapper.insert(activityData);
			}
		}
		*//*
		activityDataMapper*//*
		return "ok";
	}*/


}
